// 入口文件
import SEMRouter from "sme-router";
import axios from "axios"
import advView from "$v/adv";
import loginView from "$v/login";
import advListView from "$v/advList";
import adminListView from "$v/adminList";
import request from "./util/request"
const router = new SEMRouter("app", "html5");
window.router = router
router.route("/login", (req, res) => {
    res.render(loginView())
})
router.route("/adv", (req, res, next) => {
    next(advView({
        subRoute: res.subRoute(),
        url: req.url
    }))
})
router.route("/adv/advList", (req, res) => {
    res.render(advListView())
})
router.route("/adv/adminList", (req, res) => {
    res.render(adminListView())
    const oSaveAdminBtn = document.getElementById('saveAdminBtn');
    oSaveAdminBtn.onclick = async function () {
        const adminValue = document.adminForm.adminName.value.trim();
        const passwordValue = document.adminForm.password.value.trim();
        const rePasswordValue = document.adminForm.rePassword.value.trim();
        if (!adminValue || !passwordValue || !rePasswordValue) {
            return alert('输入框不能为空')
        }
        if (passwordValue !== rePasswordValue) {
            return alert('密码不一致，请重新输入')
        }
        try {
            const result = await request.post('/advAdmin', {
                adminName: adminValue,
                password: passwordValue,
                regTime: new Date().toLocaleString()
            })
            alert(result)
        } catch (e) {
            alert(e.message)
        }
    }
})